//
//  HomeController.m
//  项目
//
//  Created by 焦钟培 on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeController.h"

#define kBlueColor [UIColor colorWithRed:129/255.0 green: 198/255.0 blue: 221/255.0 alpha: 1.0]

@interface HomeController ()

@end

@implementation HomeController
- (void)dealloc
{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imag = [[UIImageView alloc] initWithFrame:self.view.frame];
    imag.image = [UIImage imageNamed:@"wolfgang6.jpg"];
    [self.view addSubview:imag];
    [imag release];
    [self createImage];
    // Do any additional setup after loading the view.
}
- (void)createImage
{
    if (!self.judge) {
        [self createButton];
        _judge = YES;
    }
    
    NSInteger x = arc4random()%(NSInteger)self.view.frame.size.width;
    NSInteger last = arc4random()%(NSInteger)self.view.frame.size.width;
    NSString *picStr = @"";
    NSInteger value = arc4random() % 4;
    if (value == 0) {
        picStr = [NSString stringWithFormat:@"%@fengye1.png",picStr];
    }else if (value == 1){
        picStr = [NSString stringWithFormat:@"%@fengye2.png",picStr];
    }else if (value == 2){
        picStr = [NSString stringWithFormat:@"%@fengye3.png",picStr];
    }else if (value == 3){
        picStr = [NSString stringWithFormat:@"%@fengye4.png",picStr];
    }
    if (!(x % 1)) {
        NSInteger xi = arc4random() %(NSInteger)self.view.frame.size.width + 1;
        NSInteger yi = arc4random() %(NSInteger)self.view.frame.size.width + 1;
        UIImageView *image = [[[UIImageView alloc] initWithFrame:CGRectMake(xi, - 20, 50, 50)] autorelease];
        image.image = [UIImage imageNamed:picStr];
        [self.view addSubview:image];
        if (_value % 2 == 0) {
            [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
                image.frame = CGRectMake(yi, self.view.frame.size.height, 50, 50);
            } completion:^(BOOL finished) {
                [image removeFromSuperview];
            }];
        }else{
            [image removeFromSuperview];
        }
    }
    UIImageView *iamge = [[UIImageView alloc] initWithFrame:CGRectMake(x, - 60, 60, 60)];
    iamge.image = [UIImage imageNamed:picStr];
    [self.view addSubview:iamge];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(returnAction) userInfo:nil repeats:NO];
    
    if (_value % 2 == 0 ) {
        [UIView animateWithDuration:12.0f delay:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{
            iamge.frame = CGRectMake(last, self.view.frame.size.height ,60, 60);
        } completion:^(BOOL finished) {
            [iamge removeFromSuperview];
        }];
    }else{
        
        [iamge removeFromSuperview];
    }
    
}
#warning 暂停 继续
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_value % 2 == 0) {
        [self pauseLayer:self.view.layer];
        _value++;
        }else{
        [self resumeLayer:self.view.layer];
        _value++;
        }
}

- (void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}
- (void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}
#warning 回调
- (void)returnAction
{
    [self createImage];
}
- (void)goToPlot
{
    NSLog(@"sda");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#warning createButton
- (void)createButton
{
    
    for (int i = 0; i < self.view.frame.size.height/120; i++) {
        NSInteger x = arc4random()%(NSInteger)self.view.frame.size.width / 2 + 1;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(- self.view.frame.size.width /2, (self.view.frame.size.height - 64) / 6 + 60 * i, self.view.frame.size.width /2, 50)];
        [button setBackgroundImage:[UIImage imageNamed:@"nav0.png"] forState:UIControlStateNormal];
        button.tag = 10 + i;
        [button addTarget:self action:@selector(amAction:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 25;
        [self.view addSubview:button];
        [button release];
        [UIView animateWithDuration:5.0f animations:^{
            button.frame =CGRectMake(x ,(self.view.frame.size.height - 64) / 6 + 60 * i, self.view.frame.size.width /2, 50);
        }];
    }
}
- (void)amAction:(id)sender
{
    self.view.frame = [[UIScreen mainScreen] bounds];
    self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
    UIButton *button = (UIButton *)sender;
    for (int i = 0; i < self.view.frame.size.height/120; i++) {
        UIButton *butt = (UIButton *)[self.view viewWithTag:10 + i];
        if (butt.tag != button.tag) {
            if (butt.frame.origin.x > self.view.frame.size.width/4) {
                [UIView animateWithDuration:1.0 animations:^{
                    butt.frame = CGRectMake(self.view.frame.size.width, (self.view.frame.size.height - 64) / 6 + 60 * i, self.view.frame.size.width /2, 50);
                } completion:^(BOOL finished) {
                    [butt removeFromSuperview];
                    [button removeFromSuperview];
                    _judge = NO;
                }];
            }else{
                [UIView animateWithDuration:1.0 animations:^{
                    butt.frame = CGRectMake(- self.view.frame.size.width /2, (self.view.frame.size.height - 64) / 6 + 60 * i, self.view.frame.size.width /2, 50);
                } completion:^(BOOL finished) {
                    [butt removeFromSuperview];
                    [button removeFromSuperview];
                    _judge = NO;
                }];
                
            }
            
        }
    
    }
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(plotAction) userInfo:nil repeats:NO];
    
}
- (void)plotAction
{
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
